package View.Dorm;

import Service.ChangeDormAppService;
import Service.DormService;
import Service.ServiceImpl.ChangeDormAppServiceImpl;
import Service.ServiceImpl.DormServiceImpl;
import Service.ServiceImpl.systemMsgServiceImpl;
import Service.systemMsgService;
import View.Index.Index_user;
import domain.ChangeDormApp;
import domain.Dorm;
import domain.User;
import domain.systemMsg;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.List;
import java.util.Vector;

public class ChangeDorm extends JFrame {


    public ChangeDorm(User user){
        init();
        componentInit(user);
    }

    public void init(){
        this.setTitle("宿舍更换申请");
        this.setSize(467,420);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user){
        //------------------JLabel--------------------

        JLabel label3= new JLabel();
        label3.setText("当前宿舍号:");
        label3.setBounds(75,119,71,30);
        JLabel label4= new JLabel();
        label4.setText("交换学生学号:");
        label4.setBounds(67,161,84,30);
        JLabel label5= new JLabel();
        label5.setText("交换学生姓名:");
        label5.setBounds(67,207,85,30);
        JLabel label6= new JLabel();
        label6.setText("交换的宿舍号:");
        label6.setBounds(69,253,82,30);
        JLabel label7= new JLabel();
        label7.setText("交换原因:");
        label7.setBounds(69,299,82,30);

        //------------------JTextField--------------------
        DormService service = new DormServiceImpl();
        List<Dorm> dorms = service.dormList();
        Vector<String> item2= new Vector<>();
        item2.add("请选择你的宿舍");
        for (int i = 0; i < dorms.size(); i++) {
            Dorm dorm = dorms.get(i);
            item2.add(dorm.getDormnum());
        }

        JComboBox comboBox2= new JComboBox(item2);
        comboBox2.setBounds(168,115,120,30);
        JTextField field4= new JTextField();
        field4.setText("");
        field4.setBorder(new LineBorder(Color.lightGray));
        field4.setBounds(168,163,120,30);
        JTextField field5= new JTextField();
        field5.setText("");
        field5.setBorder(new LineBorder(Color.lightGray));
        field5.setBounds(170,210,120,30);
        JComboBox comboBox3= new JComboBox(item2);
        comboBox3.setBounds(170,256,120,30);
        JTextField field7= new JTextField();
        field7.setText("");
        field7.setBorder(new LineBorder(Color.lightGray));
        field7.setBounds(170,299,120,28);

        //------------------JButton--------------------
        JButton button1= new JButton();
        button1.setText("提交申请");
        button1.setBounds(59,350,95,27);
        JButton button2= new JButton();
        button2.setText("退出");
        button2.setBounds(218,350,95,27);






        this.add(label3);
        this.add(label4);
        this.add(label5);
        this.add(label6);
        this.add(label7);
        this.add(comboBox2);
        this.add(field4);
        this.add(field5);
        this.add(comboBox3);
        this.add(field7);
        this.add(button1);
        this.add(button2);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ChangeDormApp app = new ChangeDormApp();
                app.setStunum(user.getStunum());
                app.setName(user.getUsername());
                app.setDormnum((String) comboBox2.getSelectedItem());
                app.setChangestunum(field4.getText());
                app.setChangename(field5.getText());
                app.setChangedormnum((String) comboBox3.getSelectedItem());
                app.setAppReason(field7.getText());
                app.setCheckstate("false");

                ChangeDormAppService service = new ChangeDormAppServiceImpl();
                service.addChangeDormApp(app);

                systemMsg msg = new systemMsg();
                msg.setUsername("管理员");
                msg.setTitle("学生宿舍更换申请");
                if(app.getChangename().equals("") || app.getChangename()==null){
                    msg.setNeirong("学生:"+app.getName()+" 学号:"+app.getStunum()+" 宿舍号:"+app.getDormnum()+"申请更换宿舍"+",申请原因"+app.getAppReason());
                }else {
                    msg.setNeirong("学生:"+app.getName()+" 学号:"+app.getStunum()+" 宿舍号:"+app.getDormnum()+" 申请与 学生:"+app.getChangename()+" 学号:"+app.getChangestunum()+" 宿舍号:"+app.getChangedormnum()+" 交换宿舍。申请原因:"+app.getAppReason());
                }
                msg.setSendTime(new Date());
                msg.setIsread("false");
                systemMsgService service1 = new systemMsgServiceImpl();
                service1.addMsg(msg);

                JOptionPane.showMessageDialog(new JFrame(),"提交成功请等候审核");
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Index_user(user).setVisible(true);
            }
        });
    }
}
